// 城市和小区数据
export const cityData = [
  {
    value: 'beijing',
    label: '北京',
    children: [{ value: 'beijing', label: '北京市' }],
  },
  {
    value: 'shanghai',
    label: '上海',
    children: [{ value: 'shanghai', label: '上海市' }],
  },
  {
    value: 'guangdong',
    label: '广东省',
    children: [
      { value: 'guangzhou', label: '广州市' },
      { value: 'shenzhen', label: '深圳市' },
    ],
  },
  {
    value: 'zhejiang',
    label: '浙江省',
    children: [{ value: 'hangzhou', label: '杭州市' }],
  },
  {
    value: 'jiangsu',
    label: '江苏省',
    children: [
      { value: 'nanjing', label: '南京市' },
      { value: 'suzhou', label: '苏州市' },
      { value: 'wuxi', label: '无锡市' },
    ],
  },
  {
    value: 'sichuan',
    label: '四川省',
    children: [
      { value: 'chengdu', label: '成都市' },
      { value: 'mianyang', label: '绵阳市' },
    ],
  },
];

export const communityData = {
  beijing: [
    {
      value: 'community1',
      label: '碧桂园·北京壹号',
      lat: 39.9042,
      lng: 116.4074,
    },
    { value: 'community2', label: '万科城·北京', lat: 39.9142, lng: 116.4174 },
  ],
  shanghai: [
    {
      value: 'community3',
      label: '保利心语·上海',
      lat: 31.2304,
      lng: 121.4737,
    },
    {
      value: 'community4',
      label: '融创壹号院·上海',
      lat: 31.2404,
      lng: 121.4837,
    },
  ],
  guangzhou: [
    {
      value: 'community5',
      label: '绿地中央广场·广州',
      lat: 23.1291,
      lng: 113.2644,
    },
    {
      value: 'community6',
      label: '恒大翡翠华庭·广州',
      lat: 23.1391,
      lng: 113.2744,
    },
  ],
  shenzhen: [
    {
      value: 'community7',
      label: '龙湖天街·深圳',
      lat: 22.5431,
      lng: 114.0579,
    },
    {
      value: 'community8',
      label: '中海国际社区·深圳',
      lat: 22.5531,
      lng: 114.0679,
    },
  ],
  hangzhou: [
    {
      value: 'community9',
      label: '华润悦府·杭州',
      lat: 30.2741,
      lng: 120.1551,
    },
    {
      value: 'community10',
      label: '招商雍景湾·杭州',
      lat: 30.2841,
      lng: 120.1651,
    },
  ],
  nanjing: [
    {
      value: 'community11',
      label: '绿地紫峰公馆·南京',
      lat: 32.0603,
      lng: 118.7969,
    },
    {
      value: 'community12',
      label: '万科翡翠公园·南京',
      lat: 32.0703,
      lng: 118.8069,
    },
  ],
  suzhou: [
    {
      value: 'community13',
      label: '融创桃花源·苏州',
      lat: 31.2994,
      lng: 120.5853,
    },
    {
      value: 'community14',
      label: '保利香槟国际·苏州',
      lat: 31.3094,
      lng: 120.5953,
    },
  ],
  chengdu: [
    {
      value: 'community15',
      label: '恒大绿洲·成都',
      lat: 30.5728,
      lng: 104.0668,
    },
    {
      value: 'community16',
      label: '龙湖天街·成都',
      lat: 30.5828,
      lng: 104.0768,
    },
  ],
};

// 模拟延迟
const waitTime = (time: number = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

export default {
  // 获取城市列表
  'GET /api/city/list': async (req: any, res: any) => {
    await waitTime(300);
    res.json({
      success: true,
      data: cityData,
    });
  },

  // 根据城市获取小区列表
  'GET /api/community/listByCity': async (req: any, res: any) => {
    await waitTime(300);
    const { city } = req.query;

    const communities = communityData[city as keyof typeof communityData] || [];

    res.json({
      success: true,
      data: communities,
    });
  },

  // 创建新小区
  'POST /api/community/create': async (req: any, res: any) => {
    await waitTime(500);
    const { name, address, latitude, longitude, description, city } = req.body;

    // 模拟创建小区
    const newCommunity = {
      value: `community_${Date.now()}`,
      label: name,
      lat: latitude,
      lng: longitude,
      address,
      description,
    };

    // 将新小区添加到对应城市的数据中（模拟持久化）
    if (communityData[city as keyof typeof communityData]) {
      communityData[city as keyof typeof communityData].push(newCommunity);
    }

    res.json({
      success: true,
      data: newCommunity,
      message: '小区创建成功',
    });
  },
};
